package main

import (
	_ "HeartTimesCrm/docs"
	_ "HeartTimesCrm/routers"

	"HeartTimesCrm/models"
	"encoding/json"
	"github.com/astaxie/beego"
	"strings"
)

func initBeeLogger() {
	switch strings.ToLower(beego.AppConfig.String("LogLevel")) {
	case "debug":
		beego.SetLevel(beego.LevelDebug)
	case "info":
		beego.SetLevel(beego.LevelInformational)
	case "warn":
		beego.SetLevel(beego.LevelWarning)
	case "error":
		beego.SetLevel(beego.LevelError)
	default:
		panic("LogLevel config not support!!")
	}
	if enable, err := beego.AppConfig.Bool("EnableConsoleLog"); err == nil && !enable {
		beego.BeeLogger.DelLogger("console")
	}
	if enable, err := beego.AppConfig.Bool("EnableFileLog"); err == nil && enable {
		logConfig := beego.AppConfig.String("FileLogConfig")
		var temp interface{}
		if err := json.Unmarshal([]byte(logConfig), &temp); err != nil {
			panic("LogFileConfig isn't string of json ")
		}
		beego.SetLogger("file", logConfig)
	}
}

func printConfig() {
	initBeeLogger()
	beego.BeeLogger.Info("Start Config to output.....")
	beego.BeeLogger.Info("AppName:%v", beego.AppName)
	//beego.BeeLogger.Info("AppPath:%v", beego.AppPath)
	//beego.BeeLogger.Info("workPath:%v", beego.workPath)
	//beego.BeeLogger.Info("AppConfigPath:%v", beego.AppConfigPath)
	//beego.BeeLogger.Info("EnableHttpListen:%v", beego.EnableHttpListen)
	beego.BeeLogger.Info("RunMode:%v", beego.RunMode)
	beego.BeeLogger.Info("HttpAddr:%v", beego.HttpAddr)
	beego.BeeLogger.Info("HttpPort:%v", beego.HttpPort)
	//beego.BeeLogger.Info("EnableHttpTLS:%v", beego.EnableHttpTLS)
	beego.BeeLogger.Info("HttpsPort:%v", beego.HttpsPort)
	beego.BeeLogger.Info("HttpCertFile:%v", beego.HttpCertFile)
	beego.BeeLogger.Info("HttpKeyFile:%v", beego.HttpKeyFile)
	beego.BeeLogger.Info("RecoverPanic:%v", beego.RecoverPanic)
	beego.BeeLogger.Info("SessionOn:%v", beego.SessionOn)
	beego.BeeLogger.Info("SessionProvider:%v", beego.SessionProvider)
	beego.BeeLogger.Info("SessionName:%v", beego.SessionName)
	beego.BeeLogger.Info("SessionGCMaxLifetime:%v", beego.SessionGCMaxLifetime)
	beego.BeeLogger.Info("SessionSavePath:%v", beego.SessionSavePath)
	//beego.BeeLogger.Info("SessionCookieLifeTime:%v", beego.SessionCookieLifeTime)
	beego.BeeLogger.Info("SessionAutoSetCookie:%v", beego.SessionAutoSetCookie)
	//beego.BeeLogger.Info("HttpServerTimeOut:%v", beego.HttpServerTimeOut)
	beego.BeeLogger.Info("CopyRequestBody:%v", beego.CopyRequestBody)
	beego.BeeLogger.Info("EnableAdmin:%v", beego.EnableAdmin)
	beego.BeeLogger.Info("AccessLogs:%v", beego.AccessLogs)

	beego.BeeLogger.Info("driverName:%v", beego.AppConfig.String("OrmDriverName"))
	beego.BeeLogger.Info("dataSource:%v", beego.AppConfig.String("OrmDataSource"))

	beego.BeeLogger.Info("End Config to output.....")
}

func main() {
	printConfig()
	if beego.RunMode == "dev" {
		beego.DirectoryIndex = true
		beego.StaticDir["/swagger"] = "swagger"
	}
	models.Start()
	beego.Run()
}
